//
//  GPVoiceView.m
//  Keping
//
//  Created by 23 on 2017/12/22.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPVoiceView.h"

@interface GPVoiceView ()

/**UIButton*/
@property(nonatomic,weak) UIButton *voiceButton;
/**tips*/
@property(nonatomic,weak) UILabel *tipsLabel;
/**imageView*/
@property(nonatomic,weak) UILabel *timeLabel;
@end


@implementation GPVoiceView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = COLOR(250, 250, 250);
        
        UIButton *voiceButton = [[UIButton alloc] init];
        [voiceButton setImage:[UIImage imageNamed:@"oji"] forState:UIControlStateNormal];
        [self addSubview:voiceButton];
        self.voiceButton = voiceButton;
        
        UILabel *tipsLabel = [[UILabel alloc] init];
        tipsLabel.text = @"按住说话";
        tipsLabel.textColor = [UIColor darkGrayColor];
        tipsLabel.font = [UIFont systemFontOfSize:15.0];
        [self addSubview:tipsLabel];
        self.tipsLabel = tipsLabel;
        
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.text = @"00:00";
        timeLabel.textColor = [UIColor darkGrayColor];
        timeLabel.font = [UIFont systemFontOfSize:14.0];
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        [voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
            make.width.height.mas_equalTo(100);
        }];
        
        [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(voiceButton);
            make.top.mas_equalTo(voiceButton.mas_bottom).offset(10);
        }];
        
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(voiceButton);
            make.bottom.mas_equalTo(voiceButton.mas_top).offset(-10);
        }];
        
        
        
        
        [self.voiceButton addTarget:self action:@selector(voiceInputDown:) forControlEvents:UIControlEventTouchDown];
        [self.voiceButton addTarget:self action:@selector(voiceInputUpInside:) forControlEvents:UIControlEventTouchUpInside];
        [self.voiceButton addTarget:self action:@selector(voiceInputUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
        [self.voiceButton addTarget:self action:@selector(voiceInputDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
        [self.voiceButton addTarget:self action:@selector(voiceInputDragInside:) forControlEvents:UIControlEventTouchDragInside];
        
        
    }
    return self;
}


- (void)updateVoiceTimesecond:(double)time
{
    //转换成整数秒
    int second = (int)time;
    
    //根据剩余的秒数，计算所需要的小时数、分钟数和秒数
    second--;
    int hours = second / 3600;
    int last1 = second % 3600;
    int minutes = last1 / 60;
    int seconds = last1 % 60;
    
    
    if (hours == 0) {
        self.timeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd", minutes, seconds];
    }else{
        self.timeLabel.text = [NSString stringWithFormat:@"%02zd:%02d:%02zd", hours, minutes, seconds];
    }
    
}



/**语音输入按钮被按下*/
- (void)voiceInputDown:(UIButton *)voiceButton
{
//    NSLog(@"语音输入按钮在范围内按下");
    self.tipsLabel.text = @"上滑取消";
    if ([self.delegate respondsToSelector: @selector(voiceView:startRecordVoice:)]) {
        [self.delegate voiceView:self startRecordVoice:voiceButton];
    }
}

/**语音按钮在范围内抬起*/
- (void)voiceInputUpInside:(UIButton *)voiceButton
{
//    NSLog(@"语音输入按钮在范围内抬起");
    self.tipsLabel.text = @"按住说话";
    self.timeLabel.text = @"00:00";
    if ([self.delegate respondsToSelector: @selector(voiceView:endRecordVoice:)]) {
        [self.delegate voiceView:self endRecordVoice:voiceButton];
    }
}

/**语音输入按钮在范围外抬起*/
- (void)voiceInputUpOutside:(UIButton *)voiceButton
{
//    NSLog(@"语音输入按钮在范围外抬起");
    self.tipsLabel.text = @"按住说话";
    self.timeLabel.text = @"00:00";
    if ([self.delegate respondsToSelector: @selector(voiceView:cancelRecordVoice:)]) {
        [self.delegate voiceView:self cancelRecordVoice:voiceButton];
    }
}

/**语音输入按钮被拖出了范围之外*/
- (void)voiceInputDragOutside:(UIButton *)voiceButton
{
//    NSLog(@"语音输入按钮被拖出了范围之外");
   self.tipsLabel.text = @"松开取消发送";
}

/**语音输入按钮被拖入了范围之内*/
- (void)voiceInputDragInside:(UIButton *)voiceButton
{
//    NSLog(@"语音输入按钮被拖入了范围之内");
    self.tipsLabel.text = @"上滑取消";
}

@end
